<?php
require_once('Kbs/Model/Business/Abstract.php');

class Kbs_Model_Business_Page extends Kbs_Model_Business_Abstract
{
    protected $_mapperClassName = 'Kbs_Model_Mapper_Page';

    protected $_pageId;
    protected $_pageAuthorId;
    protected $_pageCreatedAt;
    protected $_pageUpdatedAt;
    protected $_pageLinkUrl;
    protected $_pagePublished;

    /**
     * Page details
     *
     * @var array of Kbs_Model_Business_Page_Detail
     */
    protected $_pageDetails = null;

    /**
     * Page detail by current lcoale
     *
     * @var array of Kbs_Model_Business_Page_Detail
     */
    protected $_pageDetailByLocale = null;

    /**
     * Get id
     */
    public function getId()
    {
        return $this->getPageId();
    }

    public function setId($id)
    {
        return $this->setPageId($id);
    }

    /**
     * Get id
     */
    public function getPageId()
    {
        return (int)$this->_pageId;
    }

    public function setPageId($id)
    {
        $this->_pageId = (int)$id;
        return $this;
    }

    /**
     * Get page author id
     */
    public function getPageAuthorId()
    {
        return (int)$this->_pageAuthorId;
    }

    public function setPageAuthorId($id)
    {
        $this->_pageAuthorId = (int)$id;
        return $this;
    }

    /**
     * Get page create date
     */
    public function getPageCreatedAt()
    {
        return (string)$this->_pageCreatedAt;
    }

    public function setPageCreatedAt($string)
    {
        $this->_pageCreatedAt = (string)$string;
        return $this;
    }

    /**
     * Get page create date
     */
    public function getPageUpdatedAt()
    {
        return (string)$this->_pageUpdatedAt;
    }

    public function setPageUpdatedAt($string)
    {
        $this->_pageUpdatedAt = (string)$string;
        return $this;
    }

    /**
     * Get page link url title
     */
    public function getPageLinkUrl()
    {
        return (string)$this->_pageLinkUrl;
    }

    public function setPageLinkUrl($string)
    {
        $this->_pageLinkUrl = (string)$string;
        return $this;
    }

    /**
     * Get published sign
     */
    public function getPagePublished()
    {
        return (bool)$this->_pagePublished ? 1 : 0;
    }

    public function setPagePublished($isPublished)
    {
        if ((bool)$isPublished == true) {
            $this->_pagePublished = 1;
        } else {
            $this->_pagePublished = 0;
        }
        return $this;
    }

    /**
     * Fetch details for the page
     *
     *
     */
    public function fetchDetails()
    {
        if (null == $this->_pageDetails) {
            $detailObj = new Kbs_Model_Business_Page_Detail();
            $this->_pageDetails = $detailObj->findByDetailPageId($this->pageId);
        }
        return $this->_pageDetails;
    }

    /**
     * Fetch detail for the post by current locale
     *
     * @return Kbs_Model_Business_Page_Detail|false
     */
    public function fetchDetailByLocale($localeId = null)
    {
        //if (null == $this->_detailByLocale) {
        if (null == $localeId) {
            $session = new Zend_Session_Namespace('locale');
            $localeId = (int)$session->localeId;
        }
        $detailObj = new Kbs_Model_Business_Page_Detail();
        $detail = $detailObj->findByParams(array('detailPageId' => $this->pageId,
                                                 'detailLocaleId' => $localeId));
        $this->_pageDetailByLocale = $detail[0];
        //}
        return $this->_pageDetailByLocale;
    }

    /**
     * Get current date
     *
     */
    static public function datetime()
    {
        return date('Y-m-d H:i:s');
    }

    /**
     * Get the posts list by locale awared for F.O.
     *
     * @return array
     
    public function getPostsListByLocale($postCategoryId = null, $postId = null)
    {
        if (isset($postId)) {
            $posts = array($this->find($postId));
        } elseif (isset($postCategoryId)) {
            $posts = $this->findByPostCategoryId($postCategoryId);
        } else {
            $posts = $this->fetchAll();
        }

        //var_dump($posts);
        //die;

        $result = array();
        if (!empty($posts)) {
            foreach ($posts as $post) {

                // If we can't get the details then we ignore it !
                if ($post->fetchDetailByLocale()) {
                    $title = trim($post->fetchDetailByLocale()->detailTitle);
                    $content = trim($post->fetchDetailByLocale()->detailContent);
                    if (!empty($title) and !empty($content)) {
                        $result[] = $post;
                    }
                }
            }
            // Order DESC : we need the lastest to be posted first
            $result = array_reverse($result);
        }

        //var_dump($result);
        //die;
        return $result;
    }*/

    /**
     * Save page and its details
     *
     * @param array $data
     */
    public function savePageAndDetails($data)
    {
        $data['pageCreatedAt'] = !empty($data['pageCreatedAt'])
                               ? $data['pageCreatedAt']
                               : self::datetime();

        $data['pageUpdatedAt'] = !empty($data['pageUpdatedAt'])
                               ? $data['pageUpdatedAt']
                               : self::datetime();

        $this->setPageId($data['pageId'])
             ->setPageAuthorId($data['pageAuthorId'])
             ->setPageCreatedAt($data['pageCreatedAt'])
             ->setPageUpdatedAt($data['pageUpdatedAt'])
             ->setPageLinkUrl($data['pageLinkUrl'])
             ->setPagePublished($data['pagePublished']);

        //var_dump($data);die;

        // $newId is the identity (primary key)
        $newId = $this->save();

        $localeObj = new Kbs_Model_Business_Locale();
        foreach ($localeObj->fetchAll() as $locale) {

            $pageDetailObj = new Kbs_Model_Business_Page_Detail();
            $pageDetailObj->setDetailId($data['detailId'][$locale->localeId])
                          ->setDetailPageId($newId)
                          ->setDetailLocaleId($locale->localeId)
                          ->setDetailLinkLabel($data['detailLinkLabel'][$locale->localeId])
                          ->setDetailContent($data['detailContent'][$locale->localeId]);
            $pageDetailObj->save();
        }
    }

    /**
     * Delete the page and its details
     *
     */
    public function deletePageAndDetails($id)
    {
        $this->find($id);
        $this->delete();

        $detail = new Kbs_Model_Business_Page_Detail();
        $details = $detail->findByDetailPageId($id);
        if (!empty($details)) {
            foreach ($details as $detail) {
                $detail->delete();
            }
        }
    }

}